package org.idea.qiyu.live.framework.redis.starter.key;

import org.idea.qiyu.live.framework.redis.starter.key.common.RedisKeyBuilder;
import org.idea.qiyu.live.framework.redis.starter.key.common.RedisKeyLoadMatch;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-10-01
 * Time: 19:26
 */
@Configuration
@Conditional(RedisKeyLoadMatch.class)
public class IMCoreServerProviderCacheKeyBuilder extends RedisKeyBuilder {
    /**
     * 记录用户是否在线
     */
    private static final String IM_ONLINE_ZSET_KEY = "imOnlineZset";
    /**
     * 记录ack
     */
    private static final String IM_ACK_MAP_KEY = "ackMap";

    public String buildImTokenKey(Long userId, Integer appId) {
        //qiyu-live-im-core-server:imOnlineZset:appId:userId % 10000
        // 我们这里基于userId进行取模 目的是防止ZSet集合中存储大量的userId-->导致大key的问题
        // 我们这里处理方案类似于MySQL的分库分表
        return super.getPrefix() + IM_ONLINE_ZSET_KEY + super.getSplitItem() + appId + super.getSplitItem() + userId % 10000;
    }

    public String buildImAckKey(Long userId, Integer appId) {
        //qiyu-live-im-core-server:ackMap:appId:userId % 100
        // 我们这里基于userId进行取模 目的是防止ZSet集合中存储大量的userId-->导致大key的问题
        // 我们这里处理方案类似于MySQL的分库分表
        return super.getPrefix() + IM_ACK_MAP_KEY + super.getSplitItem() + appId + super.getSplitItem() + userId % 100;
    }
}